﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameInterface;

namespace SGame
{
    class GameManager : MarshalByRefObject, IGame
    {
        public event SurrenderDelegate event_player_surrender;
        public event SendGIDDelegate event_send_GID;
        public event SendChatDelegate event_send_msg;
        public event SendRdyCheck event_rdy_check;
        public event ShipHit event_ship_hit;
        public event SendStarter event_send_starter;

        public event SendNextTurn event_send_turn;

        int count = 1;
        int GID = 0;
        int size;
        string player1, player2;
        List<SGame> Games = new List<SGame>();

        public GameManager()
        {
        }

        public void newGame(int Size, string Inviter, string Invitee)
        {
            int tempID;
            if (count == 2)
            {
                GID++;
                player1 = Inviter;
                player2 = Invitee;
                size = Size;
                SGame game = new SGame(size, player1, player2, GID);
                Games.Add(game);
                tempID = game.getGameID();
                if (event_send_GID != null)
                {
                    event_send_GID(tempID);
                }
                count = 0;
            }
            count++;
        }

        public int StillThere(int GID)
        {
            int temp = 0;
            foreach (SGame game in Games)
            {
                if (game.getGameID() == GID)
                {
                    temp = game.StillThere();
                }
            }
            return temp;
        }

        public void Surrender(int GID, string playerName)
        {
            int temp; 
            foreach (SGame game in Games)
            {
                if (game.getGameID() == GID)
                {
                    temp = game.Surrender(playerName);
                }
            }
        }

        public void Start_Battle_Phase(int GID, bool Response, string playerName)
        {
            foreach (SGame game in Games)
            {
                if (game.getGameID() == GID)
                {
                    game.Start_Battle_Phase(Response, playerName);
                }
            }
        }

        public void set_ship(int GID, int type, int xcord, int ycord, bool horizontal, string whosShips)
        {
            foreach (SGame game in Games)
            {
                if (game.getGameID() == GID)
                {                    
                    game.set_ship(type, xcord, ycord, horizontal, whosShips);
                }
            }
        }

        public void chat(int GID, string message, string chatter)
        {
            foreach (SGame game in Games)
            {
                if (game.getGameID() == GID)
                {
                    if (event_send_msg != null)
                    {
                        event_send_msg(game.getGameID(), game.chat(message, chatter));
                    }
                }
            }
        }

        public void playerRdy(int GID, string Playername)
        {
            foreach (SGame game in Games)
            {
                if (game.getGameID() == GID)
                {
                    string starter = game.setCheck();     
                    if (event_rdy_check != null)
                    {
                        event_rdy_check(game.getGameID(), Playername + " is ready to play");
                    }
                    if ((starter != "-1") && (event_send_starter != null))
                    {
                        event_send_starter(game.getGameID(), starter);
                    }
                }
            }
        }

        public void checkShot(int GID, int xcord, int ycord, string player, out int deadShip)
        {
            int temp = -999;
            deadShip = temp;
            foreach (SGame game in Games)
            {
                if (game.getGameID() == GID)
                {                    
                    bool hit;
                    hit = game.checkShot(xcord, ycord, player, out temp);                    

                    if (event_ship_hit != null)
                    {
                        event_ship_hit(game.getGameID(), hit, player, xcord, ycord, temp);
                    }
                }
                else
                {
                    deadShip = -2;
                }
            }            
        }

        public void endTurn(int GID, string player)
        {
            foreach (SGame game in Games)
            {
                if (game.getGameID() == GID)
                {
                    if (event_send_turn != null)
                    {
                        event_send_turn(game.getGameID(), player);
                    }

                }
            }
        }
    }
}
